package com.estock.platform.task;

import java.util.List;

import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;

import com.estock.common.model.security.Index;
import com.estock.common.model.security.RealTimeIndex;
import com.estock.common.util.CommonUtil;
import com.estock.common.util.support.ESLogger;
import com.estock.platform.http.SinaIndexBulkConn;
import com.estock.platform.http.response.SinaRealTimeIndexBulkResponse;
import com.estock.platform.manager.DBManager;

/**
 * 
 * @author richard
 * 
 */
@Component
public class RealTimeIndexGetter extends IndexGetter {
	
	private static ESLogger logger = ESLogger
			.getLogger(RealTimeIndexGetter.class);
	
	@Override
	protected void requestAndSave(List<Index> bulkIndexes) throws Exception {
		logger.info("start requestAndSave bulkIndexes with size " + bulkIndexes.size());
		SinaRealTimeIndexBulkResponse response =  
				(SinaRealTimeIndexBulkResponse) new SinaIndexBulkConn().request(bulkIndexes, true);
		List<RealTimeIndex> realTimeIndexes = response.getRealTimeindexes();
		for (RealTimeIndex index : realTimeIndexes) {
			try {
				DBManager.getInstance().saveOrUpdate(index);
				savedNum++;
			} catch (Exception e) {
				// continue
			}
		}
	}

	public static void main(String[] args) throws JobExecutionException {
		CommonUtil.isUnitTest = true;
        
		new RealTimeIndexGetter().executeJob();
		
		CommonUtil.isUnitTest = false;
	}

}
